package microcredit.gui.editor.member;

import javax.swing.JScrollPane;

import microcredit.gui.editor.AbstractEditorForm;
import microcredit.gui.table.BeanTable;
import microcredit.gui.table.TableFactory;
import microcredit.model.Member;
import microcredit.model.MemberToTrainingType;
import microcredit.model.bo.BOTraining;
import net.miginfocom.swing.MigLayout;

public class MemberTrainingsForm extends AbstractEditorForm<Member>{
	private JScrollPane scrollPane;
	private BeanTable<MemberToTrainingType> beanTable;

	public MemberTrainingsForm() {
		initialize();
	}
	
	private void initialize() {
		setTitle("Trainings");
		setLayout(new MigLayout("", "[][grow]", "[][grow]"));
		add(getScrollPane(), "cell 1 1,grow");
	}
	
	@Override
	public void apply(Member e) throws Throwable {
		getBeanTable().load(BOTraining.create().readTrainingsByMember(e.getId()));
//		getBeanTable().load(e.getMemberToTrainingTypes());
	}

	@Override
	public Member receive(Member e) throws Throwable {
		return e;
	}

	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setViewportView(getBeanTable());
		}
		return scrollPane;
	}
	private BeanTable<MemberToTrainingType> getBeanTable() {
		if (beanTable == null) {
			beanTable = new BeanTable<MemberToTrainingType>();
			TableFactory.fill(beanTable, MemberToTrainingType.class);
		}
		return beanTable;
	}
}
